
 class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
}

class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        //一个为空一个不为空，结构不同返回false
        if(p!=null&&q==null||p==null&&q!=null){
            return false;
        }
        //两个都为空，结构数值均一致，返回true
        if(p==null&&q==null){
            return true;
        }
        //两个都不为空，结构一致比较数值,数值不同返回false
        if(p.val!=q.val){
            return false;
        }
        //两个都不为空，且结构一致数值一致，开始递归比较左子树与右子树
        return isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
    }
}